﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

/// <summary>
///Room 的摘要说明
/// </summary>
/// 
[Serializable]
public class Room : Component
{
 
    /**
     * 宾馆id
     */
    private string hotelID;

    public string HotelID
    {
        get { return hotelID; }
        set { hotelID = value; }
    }

    private string name = "";

    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    private int size = 0;

    public int Size
    {
        get { return size; }
        set { size = value; }
    }
    private float rate = 0; //占用率

    private string sizestr;

    public string Sizestr
    {
        get { return sizestr; }
        set { sizestr = value; }
    }
    public float Rate
    {
        get { return rate; }
        set { rate = value; }
    }
    private string starttime = "";

    public string Starttime
    {
        get { return starttime; }
        set { starttime = value; }
    }
    private string endtime = "";

    public string Endtime
    {
        get { return endtime; }
        set { endtime = value; }
    }
    private double price = 0;
    private string pricestr;

    public string Pricestr
    {
        get { return pricestr; }
        set { pricestr = value; }
    }
    public double Price
    {
        get { return price; }
        set { price = value; }
    }

    private string freestate;

    public string Freestate
    {
        get { return freestate; }
        set { freestate = value; }
    }

    public Room()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }
    public Room(string hotelID, string name, string pricestr, string sizestr,string starttime, string endtime, string freestate)
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
        this.hotelID = hotelID;
        this.name = name;
        this.sizestr = sizestr;
        this.pricestr = pricestr;
        this.starttime = starttime;
        this.endtime = endtime;
        this.freestate = freestate;
    }


    public bool setData(DataRow mDataRow)
    {
        if (mDataRow == null || mDataRow.ItemArray.Length < 7)
            return false;
        this.hotelID = mDataRow.ItemArray[0].ToString();
        this.name = mDataRow.ItemArray[1].ToString();
        this.starttime = mDataRow.ItemArray[2].ToString();
        this.endtime = mDataRow.ItemArray[3].ToString();

        this.size = Convert.ToInt32(mDataRow.ItemArray[4].ToString());
        this.sizestr = mDataRow.ItemArray[4].ToString();

        this.price = Convert.ToDouble(mDataRow.ItemArray[5].ToString());
        this.pricestr = mDataRow.ItemArray[5].ToString();
        this.freestate = mDataRow.ItemArray[6].ToString();

        return true;
    }

    public bool deleteSql(SqlConnection sqlcon)
    {
        string del = "DELETE FROM RoomTable WHERE HotelID="
                                + SqlFormat.ap(this.hotelID)
                                + " AND roomname=" + SqlFormat.ap(this.name)
                                + " AND startfreetime=" + SqlFormat.ap(this.starttime)
                                + " AND endfreetime=" + SqlFormat.ap(this.endtime)
                                + " AND roomsize=" + this.sizestr
                                + " AND price=" + this.pricestr
                                + " AND freestate=" + SqlFormat.ap(this.freestate);
        try
        {
            SqlDataAdapter sqldap = new SqlDataAdapter();
            sqldap.DeleteCommand = new SqlCommand(del, sqlcon);
            if (sqldap.DeleteCommand.ExecuteNonQuery() != 1)
            {
                return false;
            }
            return true;
	                
        }
        catch (System.Exception ex)
        {
            return false;
        }
        
    }


    public bool updateSql(SqlConnection sqlcon, Room room)
    {
        string upd = "UPDATE RoomTable SET "
                                + "hotelID=" + SqlFormat.ap(room.HotelID)
                                + " , roomname=" + SqlFormat.ap(room.Name)
                                + " , startfreetime=" + SqlFormat.ap(room.Starttime)
                                + " , endfreetime=" + SqlFormat.ap(room.Endtime)
                                + " , roomsize=" + room.sizestr
                                + " , price=" + room.pricestr
                                + " , freestate=" + SqlFormat.ap(room.Freestate)
                                + " WHERE HotelID=" + SqlFormat.ap(this.hotelID)
                                + " AND roomname=" + SqlFormat.ap(this.name)
                                + " AND startfreetime=" + SqlFormat.ap(this.starttime)
                                + " AND endfreetime=" + SqlFormat.ap(this.endtime)
                                + " AND roomsize=" + this.sizestr
                                + " AND price=" + this.pricestr
                                + " AND freestate=" + SqlFormat.ap(this.freestate) + ";";
        SqlDataAdapter sqldap = new SqlDataAdapter();
        sqldap.UpdateCommand = new SqlCommand(upd, sqlcon);
        if (sqldap.UpdateCommand.ExecuteNonQuery() != 1)
        {
            return false;
        }
        return true;
    }





    public override string insert(System.Data.SqlClient.SqlConnection sqlcon)
    {
        string[] values ={
                              SqlFormat.ap(this.hotelID),
                               SqlFormat.ap(this.name),
                             SqlFormat.ap(this.starttime),
                              SqlFormat.ap(this.endtime),
                               this.sizestr,
                             this.pricestr,
                              SqlFormat.ap(this.freestate)
                        };
        string insql = SqlFormat.exeProcedureNotap("inputRoomTable", values);
        //string insql = SqlFormat.insertTableNotAp("RoomTable", values);
        SqlDataAdapter sqldap = new SqlDataAdapter();
        sqldap.InsertCommand = new SqlCommand(insql, sqlcon);
        try
        {
            if (sqldap.InsertCommand.ExecuteNonQuery() != 1)
            {
                return null;
            }
        }
        catch (System.Exception ex)
        {
            return ex.Source;
        }
        

        return "ok";
    }


    public Room Clone()
    {
        MemoryStream stream = new MemoryStream();
        BinaryFormatter formatter = new BinaryFormatter();
        formatter.Serialize(stream, this);
        stream.Position = 0;
        return formatter.Deserialize(stream) as Room;
    }


    public static DataTable getFreeRoomCount(System.Data.SqlClient.SqlConnection sqlcon, string mHotelID)
    {
        try
        {
            string select = "SELECT COUNT(freestate) as canOrder,roomsize,price,startfreetime,endfreetime  FROM RoomTable WHERE hotelID="
            + SqlFormat.ap(mHotelID)
            + " AND freestate=0 "
            + " AND endfreetime>="
            + SqlFormat.ap(DateTime.Now.ToShortDateString())
            + "group by roomsize,price,startfreetime,endfreetime;";

            SqlDataAdapter sqlapt = new SqlDataAdapter();
            sqlapt.SelectCommand = new SqlCommand(select, sqlcon);
            DataTable dt = new DataTable();
            sqlapt.Fill(dt);

            return dt;
        }
        catch (System.Exception ex)
        {
            return null;
        }
        

    }
}